module mod_bivar  
  implicit none
  public init_bivar,griddata
  private
  integer,parameter :: ncp=4
  logical reinitmodbivar
  real(8),allocatable,dimension(:):: wk
  integer,allocatable,dimension(:):: iwk
contains
  subroutine init_bivar()
    use mod_para
    implicit none
    integer::ierr,niwk,nwk
    niwk=max(31,27+ncp)*ndp+nxi*nyi
    nwk=5*ndp
    allocate(iwk(1:niwk),stat=ierr)
    allocate(wk(1:nwk),stat=ierr)
    if(ierr .ne. 0) then
       write(*,*)'Error in init_bivar_workspace.'
       stop
    endif
    reinitmodbivar=.TRUE.
  endsubroutine init_bivar

  subroutine griddata(xd,yd,zd,xi,yi,zi)
    ! interp from (xd,yd,zv)-->(xi,yi,zvi)
    use mod_para
    implicit none
    real(8),dimension(ndp):: xd,yd,zd
    real(8):: xi(nxi)
    real(8):: yi(nyi)
    real(8),dimension(nxi,nyi):: zi

    if (reinitmodbivar) then
       call idsfft ( 1, ncp, ndp, xd, yd, zd, nxi, nyi, xi, yi, zi, iwk, wk )
       reinitmodbivar=.FALSE.
    else
       call idsfft ( 3, ncp, ndp, xd, yd, zd, nxi, nyi, xi, yi, zi, iwk, wk )
    endif
  endsubroutine griddata

end module mod_bivar

